<?php
class ForgotPasswordForm extends Form{
	function ForgotPasswordForm(){
		$this->link_js('javascript/jquery/packed/jquery.validate.js');
		$this->link_css('style/forgot_password.css');
		Form::Form('ForgotPasswordForm');		
		
		CGlobal::$website_title = "Khôi phục mật khẩu";
	}
	
	function on_submit(){	
		$captcha_register = Url::get('captcha_register');
		$email 			  = Url::get('email');
	
		if($email && isset($_SESSION["ecore_validate"]) && $captcha_register!='' && $captcha_register == $_SESSION["ecore_validate"]){		
			
			if($row=DB::fetch("SELECT id,user_name,email FROM account WHERE email ='$email'")){		
				$id 	= $row['id'];
				$user 	= $row['user_name'];
				$email 	= $row['email'];
				
				$link1 = '<a href="'.WEB_ROOT.ECRewrite::formatUrl('?page=forgot_password&u='.md5($user.$email).'&id='.$id).'"><b>Khôi phục Mật khẩu</b></a>';
				$link2 = '<a href="'.WEB_ROOT.ECRewrite::formatUrl('?page=forgot_password&u='.md5($user.$email).'&id='.$id).'"><b>Khoi phuc Mat khau</b></a>';
				
				$message = file_get_contents('templates/ForgotPassword/messenger.html');
				$message = str_replace('[[|link1|]]',$link1,$message);
				$message = str_replace('[[|link2|]]',$link2,$message);
				$message = str_replace('[[|user|]]',$user,$message);
				
				if(System::sendECEmail($email,'Khôi phục mật khẩu!',$message)){
					Url::redirect_current(array('action'=>'success'));
				}
				else{
					Url::redirect_current(array('action'=>'error'));
				}
			}
		}	
	}
	
	function draw(){
		/*if(isset($_POST['get_password'])){			;
			$this->on_submit();
		}*/
		global $display;
		$this->beginForm();	
		$display->add('action',Url::get('action'));
		$display->output('forgot_password');		
		$this->endForm();
	}
}
?>
